package com.example.myandroidapp1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.myandroidapp1.activity.AbstractBaseActivity;

public class InternalData extends AbstractBaseActivity implements
		OnClickListener {

	private EditText etSharedPref;
	private TextView tvShowResults;
	private FileOutputStream fos;
	private FileInputStream fis;
	private static final String FILE_NANE = "internalString";

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.bLoadSharedPref:
			String collectedData = null;
			try {
				collectedData = new LoadAsyncData().execute(FILE_NANE).get();
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (ExecutionException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			tvShowResults.setText(collectedData);
			
			break;
		case R.id.bSaveSharedPref:
			String stringData = etSharedPref.getText().toString();
			/*File f = new File(FILE_NANE);
			try {
				fos = new FileOutputStream(f);
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/

			try {
				fos = openFileOutput(FILE_NANE, Context.MODE_PRIVATE);
				fos.write(stringData.getBytes());
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		default:
			break;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sharedpreferences);

		etSharedPref = (EditText) findViewById(R.id.etSharedPref);
		tvShowResults = (TextView) findViewById(R.id.tvResultsSharedPref);

		Button loadBtn = (Button) findViewById(R.id.bLoadSharedPref);
		Button saveBtn = (Button) findViewById(R.id.bSaveSharedPref);
		loadBtn.setOnClickListener(this);
		saveBtn.setOnClickListener(this);
		try {
			fos = openFileOutput(FILE_NANE, Context.MODE_PRIVATE);
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	
	private class LoadAsyncData extends AsyncTask<String, Integer, String> {

		private ProgressDialog dialog;
		
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			dialog = new ProgressDialog(InternalData.this);
			dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			dialog.setMax(100);
			dialog.show();
		}
		
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
		}
		
		
		
		@Override
		protected void onProgressUpdate(Integer... values) {
			// TODO Auto-generated method stub
			dialog.incrementProgressBy(values[0]);
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			String collectedData = null;
			for(int i = 0; i< 20; i ++){
				publishProgress(5);
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} 
			dialog.dismiss();
			try {
				fis = openFileInput(FILE_NANE);
				byte[] dataArray = new byte[fis.available()];
				while(fis.read(dataArray) != -1){
					collectedData = new String(dataArray);
					
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return collectedData;
		}
	}

}
